package com.eric.statemachinedemo.strategy;

import com.eric.statemachinedemo.constant.ResponseCodeEnum;
import com.eric.statemachinedemo.constant.RmaStatusEnum;
import com.eric.statemachinedemo.exception.ProcessExceptionCast;
import com.eric.statemachinedemo.factory.RmaProcessStrategyFactory;
import com.eric.statemachinedemo.model.request.ProcessRequestData;
import com.eric.statemachinedemo.model.response.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 这个执行器，会牵扯到具体流程
 * 因此，选用的框架只要适配这个执行器即可
 */
@Component
public class RmaProcessExecutor {
    private static final Logger LOGGER = LoggerFactory.getLogger(RmaProcessExecutor.class);

    /**
     * 下一步
     * @param requestData
     * @return
     */
    @Transactional
    public void complete(ProcessRequestData requestData, RmaStatusEnum statusEnum){
        AbstractRmaProcessStrategy strategy = RmaProcessStrategyFactory.getStrategy(statusEnum);
        //1.校验
        ResponseResult validateResult = strategy.validate(requestData);
        if(!validateResult.isSuccess()){
            ProcessExceptionCast.cast(ResponseCodeEnum.STATE_OUTDATED);
        }
        //2.beforeComplete
        strategy.beforeComplete(requestData);
        //3.状态转换
        LOGGER.info("处理通用的业务逻辑...");
        //4.afterComplete
        strategy.afterComplete(requestData);
    }
}
